<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\REQUEST;

class Index extends Controller
{
    public function index()
    {
        //判断是否登录
//        if ( Cookie::get('adminName') && Cookie::get('adminPwd')) {
//            Session::get('adminName') = Cookie::get('adminName');
//        }elseif(Session::has('adminName'){
//            return $this->error('添加失败');
//        }

        //帮助函数写法
        return view('index/index',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img')
        ]);
    }

    //加载后台管理员列表
    public function showAdmin()
    {
        //查询gqx_admin,role,user_role表 
        // $data = Db::name('admin')->where()->order(['id'=>'asc'])->select();
        $data = Db::name('user_role')
				->alias('u')
				->join('gqx_role r','u.role_id = r.id')
				->join('gqx_admin a','u.user_id = a.id','right')
				->select();
        return view('index/alist',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img'),
                                    'data'=>$data
        ]);
    }

    // 权限管理
    public function showRole()
    {
    	$data = DB::name('role')->select();
        $access = DB::name('access')->select();
    	return view('index/mlist',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img'),
                                    'data'=>$data,
                                    'access'=>$access
        ]);
    }

    public function editAccess()
    {
    	$request = Request::instance();
		// 获取当前URL地址 不含域名
		$url = $request->url();
		if(preg_match('/\d+/',strstr($url,'/'),$result)){$result;}
    	// 增删改查role,user_role表
    	// 查看角色权限
    	$data = Db::name('role_access')
				->alias('ra')
				->where('ra.role_id ='.$result['0'])
				->join('gqx_access a','a.id = ra.access_id')
				->select();
    	return view('index/elist',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img'),
                                    'data'=>$data
        ]);
        
    }

    public function delAccess()
    {
    	$request = Request::instance();
    	$url = $request->url();
    	if(preg_match_all('/\d+/',strstr($url,'/'),$result)){$result;}
    	// $resule[0] => access_id
    	// $resule[1] => role_id
    	$data = Db::name('role_access')
    			-> where('access_id = '.$result[0][0])
    			-> where('role_id = '.$result[0][1])
    			-> delete();
    	$selectR = Db::name('role_access')
				->alias('ra')
				->where('ra.role_id ='.$result[0][0])
				->join('gqx_access a','a.id = ra.access_id')
				->select();
    	return view('index/elist',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img'),
                                    'data'=>$selectR

        ]);
    	
    }

    public function addAccess()
    {
		// 判断登陆者是否有管理者权限
		// 
		// 
		// 
		// 
		// 
    	// 如果没有权限		
    	if(/*$data['0']['role_id'] != '1'*/0){
    		echo '<script>alert("您没有添加权限!")</script>';
    	}else{
    	$request = Request::instance();
    	$url = $request->url();
    	if(preg_match('/\d+/',strstr($url,'/'),$result)){$result;}
    	// $data = DB::name('user_role')
    	// 		-> alias('ur')
    	// 		-> where('ur.user_id ='.$result['0'])
    	// 		// -> join('role r','r.id = ur.role_id')
    	// 		->select();
    	$data = DB::name('access')->select();/*var_dump($data);die;*/
    	return view('index/aalist',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img'),
                                    'data'=>$data
                ]);
    	}
    }

    public function doAdd()
    {
    	$request = Request::instance();
    	$url = $request->url();
        // var_dump($url);
    	if(preg_match_all('/\d+/',strstr($url,'/'),$result)){$result;}
     //    var_dump($result);die;
    	// $pre = $_SERVER['HTTP_REFERER'];
    	// $addid = $url;var_dump($pre);die;
    	// if(preg_match('/\d+/',strrchr($pre,'/'),$res)){$res;} // res pre result01
    	$d = ['role_id' => $result[0][1], 'access_id' => $result[0][0]];
    	$data = DB::name('role_access')->where('role_id ='.$result[0][1])->where('access_id = '.$result[0][0])->select();
    	if($data){
    		echo '<script>alert("您已有该权限")</script>';
    		$accesses = DB::name('access')->select();
    		return view('index/aalist',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img'),
                                    'data'=>$accesses
            ]);
    	}else{
    		$da = DB::name('role_access')->insert($d);
    		$accesses = DB::name('access')->select();
    		return view('index/aalist',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img'),
                                    'data'=>$accesses
                ]);
    	}

    }

    public function saveRole()
    {
        if(input('?post.name')){
            $list = Request::instance()->param();
            if($list['status']){
                $status = 1;
            }else{
                $status = 0;
            }
            // 此处还需要做数据删除与密码验证
            $data = ['role'=>$list['name'], 'status'=>$status, 'create_time'=> date('Y-m-d H:i:s')];
            DB::name('role')->insert($data);
            $roleId = Db::name('role')->getLastInsID();
            $ra = ['role_id' => $roleId, 'access_id' => $list['access_id']];
            DB::name('role_access')->insert($ra);
            $d = DB::name('role')->select();
            $access = DB::name('access')->select();
            return view('index/mlist',['CSS_PATH'=>config('admin_css'),
                                        'JS_PATH'=>config('admin_js'),
                                        'ICON_PATH'=>config('admin_icon'),
                                        'IMG_PATH'=>config('admin_img'),
                                        'data'=>$d,
                                        'access'=>$access
            ]);
        }
        else
        {
            echo '<script>alert("没有接收到数据")</script>';
        }
    }

    public function editAdmin()
    {
        $obj = request::instance();
        $param = $obj->path();
        if(preg_match('/\d+/',strrchr($param,'/'),$res)){$res;}
        $data = DB::name('user_role')
        -> alias('ur')
        -> where('ur.user_id = '.$res['0'])
        -> join('gqx_admin a','a.id = ur.user_id')
        -> join('gqx_role r', 'r.id = ur.role_id')
        -> select();
        $roles = DB::name('roles')->select();
        return view('index/ealist',['CSS_PATH'=>config('admin_css'), // 加载editadminlist, 选项卡遍历角色 注意后期判断角色是否禁用
                                        'JS_PATH'=>config('admin_js'),
                                        'ICON_PATH'=>config('admin_icon'),
                                        'IMG_PATH'=>config('admin_img'),
                                        'data'=>$data,
                                        'roles'=>$roles
            ]);
    }

    public function editRole()
    {
        $obj = request::instance();
        $param = $obj->path();
        if(preg_match('/\d+/',strrchr($param,'/'),$res)){$res;}
        $data = DB::name('role')
        -> where('id = '.$res['0'])
        -> select(); // 返回角色的id role status create_time

        return view('index/erlist',['CSS_PATH'=>config('admin_css'), 
                                        'JS_PATH'=>config('admin_js'),
                                        'ICON_PATH'=>config('admin_icon'),
                                        'IMG_PATH'=>config('admin_img'),
                                        'data'=>$data
                                        
        ]);

    }

    public function doEditRole(Request $request, $id)
    {
        $obj = $request->post();
        $status = $obj['status']?'1':'0';
        $data = ['role' => $obj['role'], 'status' => $status];
        $data = DB::name('role')->where('id',$id)->update($data);
        return view('index/index',['CSS_PATH'=>config('admin_css'),
                                    'JS_PATH'=>config('admin_js'),
                                    'ICON_PATH'=>config('admin_icon'),
                                    'IMG_PATH'=>config('admin_img')
        ]);
    }



}

